'use strict';
const { verifyJwt, loginResponse } = require('auth-token')


exports.main = async (event, context) => {
  // 需登录才能访问
  const payload = verifyJwt(event.token)
  if (!payload) {
    return loginResponse()
  }
  const currentUid = payload._id
  
  // 数据库连接
  const dbJQL = uniCloud.databaseForJQL({event, context})
  const ledgerCollection = dbJQL.collection('ume-ledger')
  
  // 业务逻辑
  switch (event.action) {
    case 'userRetrieve':
      // 用户请求自己的分类信息
      const ledger = await ledgerCollection.where(`state==true && (user_id==''||user_id=='${currentUid}')`).get()
      // console.log("ledge:", ledger.data)
      return {
        ledger: ledger.data,
        message: '获取分类成功!'
      }
      break;
    case 'userRetrieveTree':
      // 用户查询自己的分类信息，树形返回
      const treeRes = await ledgerCollection.where(`state==true && (user_id==''||user_id=='${currentUid}')`).get({getTree: true})
      // console.log("treeRes:", treeRes.data)
      return {
        ledger: treeRes.data,
        message: '获取分类成功!'
      }
      break;
      
    
    case 'userUpdate':
      const ledgerId = event._id
      const categoryName = event.category_name
      const ledgerRowRes = await ledgerCollection.doc(ledgerId).get({getOne: true});
      // console.log('ledgerRowRes:', ledgerRowRes)
      const ledgerRow = ledgerRowRes.data
      if (!ledgerRow.user_id) {
        return {
          _id: '',
          message: '系统内置分类，请不要进行修改!'
        }
      } else {
        await ledgerCollection.doc(ledgerId).update({category_name: categoryName})
        return {
          _id: ledgerId,
          message: '修改成功!'
        }
      }
      break;
    case 'userDelete':
      const delId = event._id
      const delRowRes = await ledgerCollection.where(`_id=='${delId}'`).get({getOne: true});
      const delRow = delRowRes.data
      // console.log('delRowRes:',delRowRes )
      if (!delRow.user_id) {
        return {
          _id: '',
          message: '系统内置分类，不能删除!'
        }
      }
      const childRes = await ledgerCollection.where(`parent_id=='${delRow._id}'`).get({getOne: true})
      // 是否有子集
      if (childRes.data) {
        return {
          _id: '',
          message: '该分类有子集，不能删除!'
        }
      }
      // 查询该分类下是否有账务信息
      const billRes = await dbJQL.collection('ledger-bill').where(`ledger_id=='${delRow._id}'`).get({getOne: true})
      // console.log(billRes)
      if (billRes.data) {
        return {
          _id: '',
          message: '该分类下有账务信息，不能删除!'
        }
      } else {
        // 进入删除
        ledgerCollection.doc(delId).remove()
        return {
          _id: delId,
          message: '删除成功!'
        }
      }
      break;
    case 'userCreate':
      const createData = event.ledger_data
      let newLedger;
      if (createData.parent_id) {
        // 创建子类
        newLedger = {
          category_name: createData.category_name,
          parent_id: createData.parent_id,
          user_id: currentUid
        }
      } else  {
        // 创建父类
        newLedger = {
          category_name: createData.category_name,
          parent_id: '',
          user_id: currentUid
        }
      }
      const addRes = await ledgerCollection.add(newLedger)
      return {
        _id: addRes.id,
        message: '创建分类成功!'
      }
    default:
      break
  }
  // 获取用户的分类信息
	// console.log('currentUid:', currentUid)
	//返回数据给客户端
	return {
    
  }
};
